//
// Created by Administrator on 2021/8/8.
//

#ifndef STRUCTVIEW_SIMPILEVIEW_H
#define STRUCTVIEW_SIMPILEVIEW_H

#include <cstdint>
#include "ViewInterface.h"
#include "MacroUtil.h"

namespace details {
    class SimpleView : public ViewInterface {
    public:
        SimpleView(const char* elem_name) : name{elem_name} {}

        auto equal() -> bool override {
            return false;
        }

        auto build() -> void override {
        }

        auto add_children(ViewInterface* child) -> void override {
            if (num_of_children >= MAX_SUPPORT_VIEW_ELEMENT) {
                return; // some debug info
            }
            children[num_of_children++] = child;
        }

        ~SimpleView() override {
            for (auto i = 0; i < num_of_children; i++) {
                delete children[i];
            }
        }
    private:
        uint8_t num_of_children{0};
        ViewInterface* children[MAX_SUPPORT_VIEW_ELEMENT]{nullptr};
        const char* name;
    };
}

#define __new_simple_view__(param) new details::SimpleView(TO_STRING(param))

#define __def_simple_view__(param, ...)          \
[](auto&& parent) {                              \
    auto* view = __new_simple_view__(param);     \
    __VA_ARGS__;                                 \
    return view;                                 \
}(parent.param)

#define __def_simple_view_with_parent(param, ...)  \
[](auto&& parent) {                                \
    auto* view = __new_simple_view__(param);       \
    __VA_ARGS__;                                   \
    return view;                                   \
}(parent)

#define __def_simple_view_without_parent(param, ...)  \
[](auto&& parent) {                                \
    auto* view = __new_simple_view__(param);       \
    __VA_ARGS__;                                   \
    return view;                                   \
}

#define VView(param_name, ...) __append_view__(__def_simple_view__(param_name, __VA_ARGS__))

#endif //STRUCTVIEW_SIMPILEVIEW_H
